﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class UIVADB : ViewBase
    {
        public UIVADB()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;            
        } 

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                MB = " ";
                MD = " ";
                MW = " ";
                MS = " ";
                MZ = " ";
            }
            else
            {
                string PR = dwMaster["VADPFCODE"].ToString();
                MW = PR.Substring(0, 1);
                MB = PR.Substring(1, 1);
                MD = PR.Substring(2, 1);
                MS = PR.Substring(3, 1);
                MZ = PR.Substring(4, 1); 
            } 
        }  
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            TBMain.Clear();
            if (ORDERNUM.Length < 29)
                return;
            PM["KOLL"] = ORDERNUM.Substring(0, 5);
            PM["ART"] = ORDERNUM.Substring(5, 24);
            SResult = TMV.GETVADB(PM);
            MergeData(SResult, true, true);
            if (!IsAuto)
                return;
            if (Counts > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["VADKOLL"] = ORDERNUM.Substring(0, 5);
            dwData["VADART"] = ORDERNUM.Substring(5, 24);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            TMV.InsertVADBLATT(dwData);
        }
        public virtual void OnCodeChg()
        {
            if (MV == null)
                return;
            TMV.VADPFCODE = MW + MB + MD + MS + MZ;
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                TMV.UpdateVADBLATT(row, 0);
            }
            base.OnSave();
        }
    }
    public partial class UIVADB : ViewBase
    {
        public VVADBLATT TMV
        {
            get
            {
                return (VVADBLATT)MV;
            }
        }
        #region ==MB===========
        public static readonly DependencyProperty MBProperty = DependencyProperty.Register("MB",
            typeof(string), typeof(UIVADB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMBChanged));
        public string MB
        {
            get
            {
                return (string)GetValue(MBProperty);
            }
            set
            {
                SetValue(MBProperty, value);
            }
        }
        private static void OnMBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVADB;
            if (src != null)
            {
                src.MB = (string)e.NewValue;
                src.OnCodeChg();
            }
        }
       
        #endregion
        #region ==MD===========
        public static readonly DependencyProperty MDProperty = DependencyProperty.Register("MD",
            typeof(string), typeof(UIVADB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMDChanged));
        public string MD
        {
            get
            {
                return (string)GetValue(MDProperty);
            }
            set
            {
                SetValue(MDProperty, value);
            }
        }
        private static void OnMDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVADB;
            if (src != null)
            {
                src.MD = (string)e.NewValue;
                src.OnCodeChg();
            }
        }
        #endregion
        #region ==MW===========
        public static readonly DependencyProperty MWProperty = DependencyProperty.Register("MW",
            typeof(string), typeof(UIVADB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMWChanged));
        public string MW
        {
            get
            {
                return (string)GetValue(MWProperty);
            }
            set
            {
                SetValue(MWProperty, value);
            }
        }
        private static void OnMWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVADB;
            if (src != null)
            {
                src.MW = (string)e.NewValue;
                src.OnCodeChg();
            }
        }
        #endregion
        #region ==MS===========
        public static readonly DependencyProperty MSProperty = DependencyProperty.Register("MS",
            typeof(string), typeof(UIVADB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMSChanged));
        public string MS
        {
            get
            {
                return (string)GetValue(MSProperty);
            }
            set
            {
                SetValue(MSProperty, value);
            }
        }
        private static void OnMSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVADB;
            if (src != null)
            {
                src.MS = (string)e.NewValue;
                src.OnCodeChg();
            }
        }
        #endregion
        #region ==MZ===========
        public static readonly DependencyProperty MZProperty = DependencyProperty.Register("MZ",
            typeof(string), typeof(UIVADB),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMZChanged));
        public string MZ
        {
            get
            {
                return (string)GetValue(MZProperty);
            }
            set
            {
                SetValue(MZProperty, value);
            }
        }
        private static void OnMZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVADB;
            if (src != null)
            {
                src.MZ = (string)e.NewValue;
                src.OnCodeChg();
            }
        }
        #endregion
        
    }
}
